#include "delge_squirrelgraphics_sprite.h"

void s_Graphics_Sprite_RegisterFunctions (HSQUIRRELVM v)
{
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_Create,					_SC("Sprite_Create"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_Destroy,					_SC("Sprite_Destroy"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetPosition,				_SC("Sprite_SetPosition"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetPositionX,			_SC("Sprite_GetPositionX"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetPositionY,			_SC("Sprite_GetPositionY"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetScale,				_SC("Sprite_SetScale"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetScaleX,				_SC("Sprite_GetScaleX"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetScaleY,				_SC("Sprite_GetScaleY"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetRotation,				_SC("Sprite_SetRotation"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetRotation,				_SC("Sprite_GetRotation"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetHandlePosition,		_SC("Sprite_SetHandlePosition"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetHandlePositionX,		_SC("Sprite_GetHandlePositionX"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetHandlePositionY,		_SC("Sprite_GetHandlePositionY"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetTexture,				_SC("Sprite_SetTexture"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetColor,				_SC("Sprite_SetColor"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetColorRed,				_SC("Sprite_GetColorRed"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetColorGreen,			_SC("Sprite_GetColorGreen"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetColorBlue,			_SC("Sprite_GetColorBlue"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetColorAlpha,			_SC("Sprite_GetColorAlpha"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetBlendMode,			_SC("Sprite_SetBlendMode"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetBlendMode,			_SC("Sprite_GetBlendMode"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_SetParent,				_SC("Sprite_SetParent"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_AddAnimation,			_SC("Sprite_AddAnimation"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_RemoveAnimation,			_SC("Sprite_RemoveAnimation"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_PlayAnimation,			_SC("Sprite_PlayAnimation"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_StopAnimation,			_SC("Sprite_StopAnimation"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_PauseAnimation,			_SC("Sprite_PauseAnimation"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_ResumeAnimation,			_SC("Sprite_ResumeAnimation"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_IsAnimationPlaying,		_SC("Sprite_IsAnimationPlaying"),		_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetFrameCountX,			_SC("Sprite_GetFrameCountX"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetFrameCountY,			_SC("Sprite_GetFrameCountY"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetFrameCount,			_SC("Sprite_GetFrameCount"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetFrameWidth,			_SC("Sprite_GetFrameWidth"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetFrameHeight,			_SC("Sprite_GetFrameHeight"),			_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetWidth,				_SC("Sprite_GetWidth"),					_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_GetHeight,				_SC("Sprite_GetHeight"),				_SC("*"));
	SquirrelVM::CreateFunctionGlobal(s_Graphics_Sprite_Update,					_SC("Sprite_Update"),					_SC("*"));
}

SQInteger s_Graphics_Sprite_Create (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sTextureName;
	SQInteger				iFrameWidth;
	SQInteger				iFrameHeight;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sTextureName);
	sq_getinteger			(v, 4, &iFrameWidth);
	sq_getinteger			(v, 5, &iFrameHeight);

	Texture* pTexture = ResourceManager<Texture>::GetResource(string(sTextureName));
	Sprite* pSprite = new Sprite(pTexture, iFrameWidth, iFrameHeight);

	ResourceManager<Sprite>::LoadResource(string(sSpriteName), pSprite);

	return 0;
}

SQInteger s_Graphics_Sprite_Destroy (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	ResourceManager<Sprite>::UnloadResource(sSpriteName);

	return 0;
}

SQInteger s_Graphics_Sprite_SetPosition (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQFloat					fX, fY;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getfloat				(v, 3, &fX);
	sq_getfloat				(v, 4, &fY);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetPosition(fX, fY);

	return 0;
}

SQInteger s_Graphics_Sprite_GetPositionX (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetPositionX());

	return 1;
}

SQInteger s_Graphics_Sprite_GetPositionY (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetPositionY());

	return 1;
}

SQInteger s_Graphics_Sprite_SetScale (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQFloat					fScaleX, fScaleY;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getfloat				(v, 3, &fScaleX);
	sq_getfloat				(v, 4, &fScaleY);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetScale(fScaleX, fScaleY);

	return 0;
}

SQInteger s_Graphics_Sprite_GetScaleX (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetScaleX());

	return 1;
}

SQInteger s_Graphics_Sprite_GetScaleY (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetScaleY());

	return 1;
}

SQInteger s_Graphics_Sprite_SetRotation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQFloat					fRotation;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getfloat				(v, 3, &fRotation);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetRotation(fRotation);

	return 0;
}

SQInteger s_Graphics_Sprite_GetRotation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetRotation());

	return 1;
}

SQInteger s_Graphics_Sprite_SetHandlePosition (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQFloat					fHandleX, fHandleY;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getfloat				(v, 3, &fHandleX);
	sq_getfloat				(v, 4, &fHandleY);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetHandlePosition(fHandleX, fHandleY);

	return 0;
}

SQInteger s_Graphics_Sprite_GetHandlePositionX (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetHandlePositionX());
	
	return 1;
}

SQInteger s_Graphics_Sprite_GetHandlePositionY (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushfloat			(v, pSprite->GetHandlePositionY());
	
	return 1;
}

SQInteger s_Graphics_Sprite_SetTexture (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sTextureName;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sTextureName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);
	Texture* pTexture = ResourceManager<Texture>::GetResource(sTextureName);

	pSprite->SetTexture(pTexture);

	return 0;
}

SQInteger s_Graphics_Sprite_SetColor (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQInteger				iRed, iGreen, iBlue;
	SQInteger				iAlpha;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getinteger			(v, 3, &iRed);
	sq_getinteger			(v, 4, &iGreen);
	sq_getinteger			(v, 5, &iBlue);
	sq_getinteger			(v, 6, &iAlpha);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetColor(iRed, iGreen, iBlue, iAlpha);

	return 0;
}

SQInteger s_Graphics_Sprite_GetColorRed (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetColor()[0]);

	return 1;
}

SQInteger s_Graphics_Sprite_GetColorGreen (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetColor()[1]);

	return 1;
}

SQInteger s_Graphics_Sprite_GetColorBlue (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetColor()[2]);

	return 1;
}

SQInteger s_Graphics_Sprite_GetColorAlpha (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetColor()[3]);

	return 1;
}

SQInteger s_Graphics_Sprite_SetBlendMode (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQInteger				eBlendMode;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getinteger			(v, 3, &eBlendMode);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->SetBlendMode((DelGEGraphicsBlendMode)eBlendMode);

	return 0;
}

SQInteger s_Graphics_Sprite_GetBlendMode (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, (int)pSprite->GetBlendMode());

	return 1;
}

SQInteger s_Graphics_Sprite_SetParent (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sParentSpriteName;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sParentSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);
	Sprite* pParent = ResourceManager<Sprite>::GetResource(sParentSpriteName);

	pSprite->SetParent(pParent);

	return 0;
}

SQInteger s_Graphics_Sprite_AddAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sAnimationName;
	SQInteger				iStartFrame, iEndFrame;
	SQFloat					fFPS;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sAnimationName);
	sq_getinteger			(v, 4, &iStartFrame);
	sq_getinteger			(v, 5, &iEndFrame);
	sq_getfloat				(v, 6, &fFPS);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);
	
	pSprite->AddAnimation(sAnimationName, iStartFrame, iEndFrame, fFPS);

	return 0;
}

SQInteger s_Graphics_Sprite_RemoveAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sAnimationName;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sAnimationName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->RemoveAnimation(sAnimationName);

	return 0;
}

SQInteger s_Graphics_Sprite_PlayAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sAnimationName;
	SQInteger				ePlaystyle;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sAnimationName);
	sq_getinteger			(v, 4, &ePlaystyle);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	cout << sSpriteName << endl;
	cout << ePlaystyle << endl;

	pSprite->PlayAnimation(sAnimationName, (DelGEGraphics_Animation_Playstyle)ePlaystyle);

	return 0;
}

SQInteger s_Graphics_Sprite_StopAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	if (pSprite != NULL)
		pSprite->StopAnimation();

	return 0;
}

SQInteger s_Graphics_Sprite_PauseAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->PauseAnimation();

	return 0;
}

SQInteger s_Graphics_Sprite_ResumeAnimation (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->ResumeAnimation();

	return 0;
}

SQInteger s_Graphics_Sprite_IsAnimationPlaying (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	const SQChar			*sAnimationName;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getstring			(v, 3, &sAnimationName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushbool				(v, pSprite->IsAnimationPlaying(sAnimationName));

	return 1;
}

SQInteger s_Graphics_Sprite_GetFrameCountX (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetFrameCountX());

	return 1;
}

SQInteger s_Graphics_Sprite_GetFrameCountY (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetFrameCountY());

	return 1;
}

SQInteger s_Graphics_Sprite_GetFrameCount (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetFrameCount());

	return 1;
}

SQInteger s_Graphics_Sprite_GetFrameWidth (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetFrameWidth());

	return 1;
}

SQInteger s_Graphics_Sprite_GetFrameHeight (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetFrameHeight());

	return 1;
}

SQInteger s_Graphics_Sprite_GetWidth (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetWidth());

	return 1;
}

SQInteger s_Graphics_Sprite_GetHeight (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;

	sq_getstring			(v, 2, &sSpriteName);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	sq_pushinteger			(v, pSprite->GetHeight());

	return 1;
}

SQInteger s_Graphics_Sprite_Update (HSQUIRRELVM v)
{
	const SQChar			*sSpriteName;
	SQFloat					fDelta;

	sq_getstring			(v, 2, &sSpriteName);
	sq_getfloat				(v, 3, &fDelta);

	Sprite* pSprite = ResourceManager<Sprite>::GetResource(sSpriteName);

	pSprite->Update(fDelta);
	
	return 0;
}